package com.ruoyi.framework.shiro.web.filter;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

//拦截前会员是否登录
public class UserFilter extends AccessControlFilter {


    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {

        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {

        Subject subject = getSubject(servletRequest, servletResponse);

        if(!subject.isAuthenticated()){

            String json = JSON.toJSONString(AjaxResult.error("请先登录!"));
            servletResponse.setContentType("application/json;charset=utf-8");
            servletResponse.getWriter().println(json);

            return false;
        }

        return true;
    }


}
